﻿using GKQC.DbHelper.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace GKQC.DbHelper.Services
{
    public class UserRightService
    {
       public static bool CheckRight(Form form, UserModel user, RightType rightType)
        {
            bool flag = false;

            flag = CheckRight(form.Name, user, rightType);

            //if (user.IsAdmin) flag = true;
            //else //if (user.UserRights != null || user.UserRights.Count > 0)
            //{
            //    string rightName = Enum.GetName(typeof(RightType), rightType);
            //    UserRightModel model = user.UserRights.Find(u => u.FormCode == form.Name);
            //    if (model != null) 
            //    {
            //        string rightValue = model.GetType().GetProperty(rightName).GetValue(model, null).ToString();
            //        if (rightValue == "1") flag = true;
            //    }

            //    if(!flag)
            //    {
            //        model = user.UserGroupRights.Find(u => u.FormCode == form.Name);
            //        if (model != null)
            //        {
            //            string rightValue = model.GetType().GetProperty(rightName).GetValue(model, null).ToString();
            //            if (rightValue == "1") flag = true;
            //        }
            //    }
            //}

            //if(!flag)
            //{
            //    MessageBox.Show("權限不足！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            //}

            return flag;
        }

        public static bool CheckRight(string formName, UserModel user, RightType rightType)
        {
            bool flag = false;

            if (user.IsAdmin) flag = true;
            else //if (user.UserRights != null || user.UserRights.Count > 0)
            {
                string rightName = Enum.GetName(typeof(RightType), rightType);
                UserRightModel model = user.UserRights.Find(u => u.FormCode == formName);
                if (model != null)
                {
                    string rightValue = model.GetType().GetProperty(rightName).GetValue(model, null).ToString();
                    if (rightValue == "1") flag = true;
                }

                if (!flag)
                {
                    model = user.UserGroupRights.Find(u => u.FormCode == formName);
                    if (model != null)
                    {
                        string rightValue = model.GetType().GetProperty(rightName).GetValue(model, null).ToString();
                        if (rightValue == "1") flag = true;
                    }
                }
            }

            if (!flag)
            {
                MessageBox.Show("權限不足！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            return flag;
        }
    }

    public enum RightType
    {
        
        Run,
        Add,
        Modi,
        Delete,
        Query,
        Export,
        Approve,
        UnApprove
    }
}
